var one = 0;
var tow = 0;
var three = 0;

//百度地图
// 百度地图API功能
var map = new BMap.Map("allmap");
	
var list = [];
//用户所在的区县
var area = 0;
var city = 0;
//当前网点
getUserName()
function getUserName() {
    $.ajax({
        url: ajax_host + 'getUserName.act',
        dataType: 'JSON',
        success: function (resp) {
            if (resp.success) {
                console.log(resp.user)
                area = resp.user.area;
                city = resp.user.city;
                findAddress(area, city);
            } else {
                layer.alert('获取用户名失败')
            }
        },
        error: function (resp) {
            layer.alert('获取用户名失败')
        }
    });
}
var areaName = '';
var cityName = '';
var longg = 0;
var lat = 0;
function findAddress(area, city) {
    $.ajax({
        url: ajax_host + 'findAddress.act',
        dataType: 'JSON',
        data: {
            area: area,
            city: city
        },
        success: function (resp) {
            if (resp.success) {
                areaName = resp.addressinfo.area;
                cityName = resp.addressinfo.city;
                longg = resp.addressinfo.longg;
                lat = resp.addressinfo.lat;
                
                var new_point1 = new BMap.Point(longg,lat);
                map.centerAndZoom(new_point1,15);
                var marker1 = new BMap.Marker(new_point1);  // 创建标注
                map.addOverlay(marker1);              // 将标注添加到地图中

                console.log('area' + areaName);
                console.log('city' + cityName);
                html('areaName', resp.addressinfo.area);
                html('cityName', resp.addressinfo.city);
            } else {
                layer.alert('获取用户地点失败')
            }
        },
        error: function (resp) {
            layer.alert('获取用户地点失败')
        }
    });
}


getaddressone();
function getaddressone() {
    console.log('开始获取省级列表')
    $.ajax({
        url: ajax_host + 'getaddressone.act',
        dataType: 'JSON',

        success: function (resp) {
            if (resp.success) {
                addressonelist = resp.list;

                console.log("列表：" + addressonelist);
                renderAddressOne(addressonelist)
            }
        }
    })
}
function renderAddressOne(arr) {
    var str = '<option value="0">请选择</option>';
    for (var i = 0; i < arr.length; i++) {
        str += `<option  value="${arr[i].nId}">${arr[i].nName}</option>`;
    }
    html('addressOne', str);
}
function change() {
    one = val('addressOne');
    console.log('one' + one);
    $.ajax({
        url: ajax_host + 'getaddressTow.act',
        dataType: 'JSON',
        data: {
            fid: one
        },
        success: function (resp) {
            if (resp.success) {
                addressTowlist = resp.list;
                renderAddressTow(addressTowlist);
            } else {
                layer.alert(resp.errmsg);
            }
        }
    })
}
function renderAddressTow(arr) {
    var str = '<option value="0">请选择</option>';
    for (var i = 0; i < arr.length; i++) {
        str += `<option  value="${arr[i].nId}">${arr[i].nName}</option>`;
    }
    html('addressTwo', str);
}
function change2() {
    tow = val('addressTwo');
    console.log('tow' + tow);
    $.ajax({
        url: ajax_host + 'getaddressThree.act',
        dataType: 'JSON',
        data: {
            fid: tow
        },
        success: function (resp) {
            if (resp.success) {
                addressThreelist = resp.list;
                renderAddressThree(addressThreelist);
            } else {
                layer.alert(resp.errmsg);
            }
        }
    })
}
function renderAddressThree(arr) {
    var str = '<option value="0">请选择</option>';
    for (var i = 0; i < arr.length; i++) {
        str += `<option  value="${arr[i].nId}">${arr[i].nName}</option>`;
    }
    html('addressThree', str);
}


function change3() {
    three = val('addressThree');
    console.log('three' + three);

}

function toSend(){
    $.ajax({
		url:ajax_host+'getaddressln.act',
        dataType:'JSON',
        data: {
            one : one,
            tow : tow,
            three : three
        },
    	success:function(resp){
    		if(resp.success){
                longg = resp.addressln.nLong;
                lat = resp.addressln.nLat;
                console.log('网点经度'+longg);
                console.log('网点维度'+lat);
                theLocation(longg,lat)
    		}else{
                layer.alert('获取网点失败')
            }
    	},
    	
	});
}





map.enableScrollWheelZoom(true);
	// 用经纬度设置地图中心点
	function theLocation(x,y){
		if(x != "" && y != ""){
			map.clearOverlays(); 
			var new_point = new BMap.Point(x,y);
			var marker = new BMap.Marker(new_point);  // 创建标注
			map.addOverlay(marker);              // 将标注添加到地图中
			map.panTo(new_point);      
		}
    }
    	//添加地图类型控件
	map.addControl(new BMap.MapTypeControl({
		mapTypes:[
            BMAP_NORMAL_MAP,
            BMAP_HYBRID_MAP
        ]}));	